1 using UnityEngine;
2 using
System.Collections;
3 using
UnityStandardAssets.Vehicles.Car;
4
5 [RequireComponent(
typeof(Rigidbody))]
6 public
class Motor : MonoBehaviour {
7
8     
public timer tim;
9
10     
private bool respawned = false;
11     
private bool waitframe = true;
12
13     
private Vector3 startpos;
14     
private Quaternion startrot;
15
16     
public Color color;
17
18     
public bool crossmiddle = false;
19     
public int lap = 1;
20
21     
public float enginepower = 200f;
22     
public float turnpower = 20f;
23     
public float brake = 1000f;
24
25     
public bool hittingFloor = false;
26
27     
public Rigidbody m_Rigidbody;
28     
public Wheel FRWheel;
29     
public Wheel FLWheel;
30     
public Wheel BRWheel;
31     
public Wheel BLWheel;
32
33     [Range(
0, 1)] public float m_SteerHelper;
34
35     
public WheelCollider[] m_WheelColliders = new WheelCollider[4];
36     
public WheelEffects[] m_WheelEffects = new WheelEffects[4];
37
38     
public float MaxSpeed = 200;
39     
public float Revs = 0;
40     
public float m_SlipLimit = 0.3f;
41
42     
public float CurrentSpeed { get { return m_Rigidbody.velocity.magnitude * 2.23693629f; } }
43     
public float AccelInput { get; private set; }
44
45     
private float m_OldRotation;
46
47
48     
private void Start()
49     {
50         tim = transform.root.GetComponentInChildren<timer>();
51         m_Rigidbody = GetComponent<Rigidbody>();
52         startpos = transform.position;
53         startrot = transform.rotation;
54     }
55
56     
public void resetIt()
57     {
58         transform.rotation = startrot;
59         transform.position = startpos;
60         respawned =
true;
61         crossmiddle =
false;
62         lap =
1;
63
64         
for (int i = 0; i < 4; i++)
65         {
66             m_WheelColliders[i].motorTorque =
0;
67             m_WheelColliders[i].steerAngle =
0;
68             m_WheelColliders[i].steerAngle =
0;
69             m_WheelColliders[i].brakeTorque = Mathf.Infinity;
70         }
71         m_Rigidbody.isKinematic =
true;
72         respawned =
true;
73         waitframe =
false;
74     }
75
76     
void LateUpdate()
77     {
78         
// (if the vehicle has been respanwed this frame,
79         
// then a variable respawned is set to true)
80
81         
if (waitframe && respawned)
82         {
83             waitframe =
false;
84             
for (int i = 0; i < 4; i++)
85             {
86                 m_WheelColliders[i].brakeTorque =
0;
87             }
88             m_Rigidbody.isKinematic =
false;
89             respawned =
false;
90         }
91         
else if (!waitframe && respawned)
92         {
93             waitframe =
true;
94         }
95     }
96
97     
void FixedUpdate()
98     {
99
100         
101
102         hittingFloor =
false;
103         
for (int i = 0; i < 4; i++)
104         {
105             
if (m_WheelColliders[i].isGrounded)
106             {
107                 hittingFloor =
true;
108                 
break;
109             }
110         }
111
112         Revs = Mathf.Lerp(Revs, Mathf.Abs(CurrentSpeed /
200), Time.deltaTime * 5f);
113
114         CheckForWheelSpin();
115         SteerHelper();
116
117         
float posx = Mathf.Clamp(transform.position.x,10,240);
118         
float posz = Mathf.Clamp(transform.position.z,10,340);
119
120         transform.position =
new Vector3(posx, transform.position.y,posz);
121
122
123     }
124
125     
public void Move(float turn, float accel, float thebrake, float other)
126     {
127         
float torque = accel * enginepower;
128
129         AccelInput = Mathf.Clamp(torque,
0, 1);
130
131         FRWheel.Move(torque);
132         FLWheel.Move(torque);
133         BRWheel.Move(torque);
134         BLWheel.Move(torque);
135
136         
float steer = turn * turnpower;
137
138         FRWheel.Turn(steer);
139         FLWheel.Turn(steer);
140
141         
for (int i = 0; i < 4; i++)
142         {
143             
if (CurrentSpeed > 5)
144             {
145                 m_WheelColliders[i].brakeTorque = brake * thebrake;
146             }
147             
//else if (footbrake > 0)
148             
//{
149             
// m_WheelColliders[i].brakeTorque = 0f;
150             
// m_WheelColliders[i].motorTorque = -enginepower * footbrake;
151             
//}
152         }
153
154     }
155
156
157     
public void HandBrake(float val)
158     {
159
160         
for (int i = 0; i < 4; i++)
161         {
162             WheelFrictionCurve fc = m_WheelColliders[i].sidewaysFriction;
163             
164             fc.extremumSlip = Mathf.Lerp(
0.1f, 0.4f, val);
165             fc.asymptoteSlip = Mathf.Lerp(
0.2f, 0.5f, val);
166             m_WheelColliders[i].sidewaysFriction = fc;
167         }
168
169     }
170
171     
private void CheckForWheelSpin()
172     {
173         
// loop through all wheels
174
175         
for (int i = 0; i < 4; i++)
176         {
177             WheelHit wheelHit;
178             m_WheelColliders[i].GetGroundHit(
out wheelHit);
179
180             
// is the tire slipping above the given threshhold
181             
if (Mathf.Abs(wheelHit.sidewaysSlip) >= m_SlipLimit)
182             {
183                 
// avoiding all four tires screeching at the same time
184                 
// if they do it can lead to some strange audio artefacts
185                 
if (!AnySkidSoundPlaying())
186                 {
187                     m_WheelEffects[i].PlayAudio();
188                 }
189                 
continue;
190             }
191
192             
// if it wasnt slipping stop all the audio
193             
if (m_WheelEffects[i].PlayingAudio)
194             {
195                 m_WheelEffects[i].StopAudio();
196             }
197             
// end the trail generation
198             m_WheelEffects[i].EndSkidTrail();
199         }
200     }
201
202     
private bool AnySkidSoundPlaying()
203     {
204         
for (int i = 0; i < 4; i++)
205         {
206             
if (m_WheelEffects[i].PlayingAudio)
207             {
208                 
return true;
209             }
210         }
211         
return false;
212     }
213
214     
private void SteerHelper()
215     {
216         
for (int i = 0; i < 4; i++)
217         {
218             WheelHit wheelhit;
219             m_WheelColliders[i].GetGroundHit(
out wheelhit);
220             
if (wheelhit.normal == Vector3.zero)
221                 
return; // wheels arent on the ground so dont realign the rigidbody velocity
222         }
223
224         
// this if is needed to avoid gimbal lock problems that will make the car suddenly shift direction
225         
if (Mathf.Abs(m_OldRotation - transform.eulerAngles.y) < 10f)
226         {
227             
var turnadjust = (transform.eulerAngles.y - m_OldRotation) * m_SteerHelper;
228             Quaternion velRotation = Quaternion.AngleAxis(turnadjust, Vector3.up);
229             m_Rigidbody.velocity = velRotation * m_Rigidbody.velocity;
230         }
231         m_OldRotation = transform.eulerAngles.y;
232     }
233
234     
public void hitMark(Transform mark)
235     {
236         
if (mark.name == "middle")
237         {
238             crossmiddle =
true;
239         }
240         
if (crossmiddle && mark.name == "begin")
241         {
242             crossmiddle =
false;
243             lap++;
244             
if (lap == tim.laps + 1)
245             {
246                 SendMessage(
"finished");
247             }
248             tim.addlap(transform,lap,color);
249         }
250     }
251
252 }


Gõ tìm kiếm nhanh...